package com.xiyuan.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.xiyuan.dao.HuiyuanMapper;
import com.xiyuan.dao.PipeiMapper;
import com.xiyuan.entity.Huiyuan;
import com.xiyuan.entity.Pipei;

/**
 * @author：wujinxing @since：2016-2-27 下午6:25:14
 * @version: 1.0
 * @function:
 */
@Controller
public class PipeiAction {

	@Autowired
	private PipeiMapper pipeiMapper;

	@Autowired
	private HuiyuanMapper huiyuanMapper;

	@RequestMapping(value = "/pipei_add.jsp", method = RequestMethod.GET)
	public String add(ModelMap modelMap) {

		return "/pipei_add";
	}

	@RequestMapping(value = "/pipei_add.jsp", method = RequestMethod.POST)
	public String addPost(ModelMap modelMap, @ModelAttribute("pipeiForm") Pipei pipei) {

		String message = null;
		Huiyuan hy1 = huiyuanMapper.selectByPrimaryKey(pipei.getHyid1());
		Huiyuan hy2 = huiyuanMapper.selectByPrimaryKey(pipei.getHyid2());
		if (hy1 == null || hy2 == null) {
			message = "ID号输入错误，请检查";
		} else {
			if (new String("女").equals(hy1.getSex())) {
				message = "男方资料性别错误";
			}
			pipei.setName1(hy1.getName());

			if (new String("男").equals(hy2.getSex())) {
				message = "女方资料性别错误";
			}
			pipei.setName2(hy2.getName());

			if (message == null) {
				message = "添加成功";
				pipeiMapper.insertSelective(pipei);
			}
		}
		modelMap.put("message", message);
		modelMap.put("pipei", pipei);
		return "/pipei_add";
	}

	@RequestMapping(value = "/pipei_list.jsp")
	public String list(ModelMap modelMap, @RequestParam(value = "id", required = true) String id) {

		List<Pipei> pipeis = pipeiMapper.selectByHyid(Integer.valueOf(id));
		modelMap.put("pipeis", pipeis);
		modelMap.put("id", id);
		return "/pipei_list";
	}
}
